package com.ruoyi.web.controller.wms;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsInList;
import com.ruoyi.wms.service.IWmsInListService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 入库记录Controller
 * 
 * @author yy
 * @date 2024-02-15
 */
@Controller
@RequestMapping("/wms/inlist")
public class WmsInListController extends BaseController
{
    private String prefix = "wms/inlist";

    @Autowired
    private IWmsInListService wmsInListService;

    @RequiresPermissions("wms:inlist:view")
    @GetMapping()
    public String inlist(ModelMap mmap)
    {
        WmsInList vo = wmsInListService.selectWmsInListCount();
        if(vo ==null){
            vo = new WmsInList();
        }
        if(vo.getInQuantity() == null){
            vo.setInQuantity(new BigDecimal(0));
        }
        if(vo.getRepairQuantity() == null){
            vo.setRepairQuantity(new BigDecimal(0));
        }
        if(vo.getScrapQuantity() == null){
            vo.setScrapQuantity(new BigDecimal(0));
        }
        mmap.put("wmsInList",vo);
        return prefix + "/inlist";
    }

    /**
     * 查询入库记录列表
     */
    @RequiresPermissions("wms:inlist:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsInList wmsInList)
    {
        startPage();
        if(!ShiroUtils.getSysUser().isAdmin()){
            wmsInList.setDeptId(ShiroUtils.getSysUser().getDeptId());
        }
        List<WmsInList> list = wmsInListService.selectWmsInListList(wmsInList);
        return getDataTable(list);
    }

    /**
     * 导出入库记录列表
     */
    @RequiresPermissions("wms:inlist:export")
    @Log(title = "入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsInList wmsInList)
    {
        List<WmsInList> list = wmsInListService.selectWmsInListList(wmsInList);
        ExcelUtil<WmsInList> util = new ExcelUtil<WmsInList>(WmsInList.class);
        return util.exportExcel(list, "入库记录数据");
    }

    /**
     * 新增入库记录
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("userName", ShiroUtils.getSysUser().getUserName());
        mmap.put("createTime", DateUtils.parseDateToStr("yyyy-MM-dd",new Date()));
        return prefix + "/add";
    }

    /**
     * 新增保存入库记录
     */
    @RequiresPermissions("wms:inlist:add")
    @Log(title = "入库记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsInList wmsInList)
    {
        return toAjax(wmsInListService.insertWmsInList(wmsInList));
    }

    /**
     * 修改入库记录
     */
    @RequiresPermissions("wms:inlist:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WmsInList wmsInList = wmsInListService.selectWmsInListById(id);
        mmap.put("wmsInList", wmsInList);
        return prefix + "/edit";
    }

    /**
     * 修改保存入库记录
     */
    @RequiresPermissions("wms:inlist:edit")
    @Log(title = "入库记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsInList wmsInList)
    {
        return toAjax(wmsInListService.updateWmsInList(wmsInList));
    }

    /**
     * 删除入库记录
     */
    @RequiresPermissions("wms:inlist:remove")
    @Log(title = "入库记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsInListService.deleteWmsInListByIds(ids));
    }
}
